package com.hptpd.questionnairesurvey.vo;

import com.google.common.collect.Lists;
import com.hptpd.questionnairesurvey.domain.Dimensionality;
import com.hptpd.questionnairesurvey.domain.Questionnaire;
import com.hptpd.questionnairesurvey.domain.question.BaseQuestion;
import com.hptpd.questionnairesurvey.utils.AbstractMyBeanUtils;
import lombok.Data;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Data
public class DimensionalityVo {
    private String id;

    private String name;

    private String weight;

    private Integer sort;

    private boolean edit = false;

    private Integer questionNum = 0;
    /**
     * 维度暂时生成的标识
     */
    private String dimensionTempTag;

    private List<DimensionalityVo> children = new ArrayList<>();

    private DimensionalityVo parentDimensionalityVo;

    private List<QuestionVo> questions = new ArrayList<>();

    public static DimensionalityVo toVo(Dimensionality dimensionality, QuestionnaireVo questionnaireVo) {
        DimensionalityVo dimensionalityVo = new DimensionalityVo();
        AbstractMyBeanUtils.copyProperties(dimensionality, dimensionalityVo);
        dimensionalityVo.setQuestions(QuestionVo.toVos(dimensionality.getBaseQuestions(), questionnaireVo, dimensionalityVo));
        if (!dimensionality.getChildDimensionalities().isEmpty()) {
            for (Dimensionality dimension :
                    dimensionality.getChildDimensionalities()) {
                dimensionalityVo.getChildren().add(toVo(dimension, questionnaireVo));
            }
        }
        dimensionalityVo.setQuestionNum(dimensionalityVo.getQuestions().size());
        return dimensionalityVo;
    }

    public static List<DimensionalityVo> toVos(List<Dimensionality> dimensionalities, QuestionnaireVo questionnaireVo) {
        List<DimensionalityVo> dimensionalityVos = new ArrayList<>();
        for (Dimensionality dimension :
                dimensionalities) {
            dimensionalityVos.add(toVo(dimension, questionnaireVo));
        }
        return dimensionalityVos;
    }

    public static Dimensionality getRootDimensionality(Dimensionality dimensionality) {
        return getParentDimensionality(dimensionality);
    }

    private static Dimensionality getParentDimensionality(Dimensionality dimensionality) {
        if (null == dimensionality.getParentDimensionality()) {
            return dimensionality;
        }
        return getParentDimensionality(dimensionality.getParentDimensionality());
    }
}
